﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Vjia.Framework.Configuration;
using Vjia.Framework.MVC;
using System.Web;

namespace Vjia.Framework
{
    public class Urls
    {
        public static string BuildCss(FileItem fileItem)
        {
            return BuilrRSUrl(ConfigMananger.WebSiteConfig.Resource.Css, fileItem);
        }

        public static string BuildJs(FileItem fileItem)
        {
            return BuilrRSUrl(ConfigMananger.WebSiteConfig.Resource.Js, fileItem);
        }

        public static string BuildImg(FileItem fileItem)
        {
            return BuilrRSUrl(ConfigMananger.WebSiteConfig.Resource.Img, fileItem);
        }

        public static string BuildImg(RSPathType rsPathType, string path)
        {
            FileItem fileItem = new FileItem();
            fileItem.Path = path;
            fileItem.SecurePath = path;
            fileItem.InnerPathType = rsPathType.ToString();
            return BuilrRSUrl(ConfigMananger.WebSiteConfig.Resource.Img, fileItem);
        }


        private static string BuilrRSUrl(ResourceItem resourceItem, FileItem fileItem)
        {

            string path = GetCurrentRSPath(fileItem, CurrentHttpType);
            string url = string.Empty;
            string demoPrefix = string.Empty;
            if (fileItem == null)
                return string.Empty;
            string hostAddress = GetCurrentHostAddress(fileItem.PathType, resourceItem);

            string httpPrefix = string.Format("{0}://", CurrentHttpType.ToString().ToLower());
            string version = string.Empty;
        
            url = hostAddress + path;
            PrefixType prefixType = GetDemoPrefix();
            if (prefixType != PrefixType.Formal)
            {
                if (fileItem.PathType == RSPathType.Network)
                {
                    demoPrefix = string.Format("{0}://{1}", CurrentHttpType.ToString(), prefixType.ToString().ToLower());
                    url = demoPrefix + url.Substring(httpPrefix.Length);
                    
                }
            }
            if (!string.IsNullOrEmpty(fileItem.Version))
            {
                version = url.Contains("?") ? "&" : "?";
                version += string.Format("v={0}", fileItem.Version);
            }
            url = url + version;
            return url;
        }
  

        private static string GetCurrentHostAddress(RSPathType rsPathType, ResourceItem resourceItem)
        {
            string hostAddress = string.Empty;
            string host = string.Empty;
            string port = string.Empty;
            if (rsPathType == RSPathType.Network)
            {
                if (CurrentHttpType == HttpType.Https)
                {
                    hostAddress = resourceItem.SecureAddress;
                }
                else
                {
                    hostAddress = resourceItem.Address;
                }
            }
            else
            {
                host = System.Web.HttpContext.Current.Request.Url.Host;
                port = string.Format(":{0}", System.Web.HttpContext.Current.Request.Url.Port.ToString());
                if (port == ":80")
                {
                    port = string.Empty;
                }

                hostAddress = string.Format("{0}://{1}{2}", CurrentHttpType.ToString().ToLower(), System.Web.HttpContext.Current.Request.Url.Host, port);
            }
            return hostAddress;
        }

        private static string GetCurrentRSPath(FileItem fileItem, HttpType httpType)
        {
            string path = fileItem.Path;
            if (httpType == HttpType.Https && string.IsNullOrEmpty(fileItem.SecurePath))
            {
                path = fileItem.SecurePath;
            }
            return path;
        }

        private static PrefixType GetDemoPrefix()
        {
             string httpPrefix = string.Format("{0}://", CurrentHttpType.ToString().ToLower());
             string demoPrefix = string.Format("{0}://{1}", CurrentHttpType.ToString().ToLower(), PrefixType.Demo.ToString().ToLower());
             string vssPrefix = string.Format("{0}://{1}", CurrentHttpType.ToString().ToLower(), PrefixType.Vss.ToString().ToLower());
            if (System.Web.HttpContext.Current.Request.Url.ToString().ToLower().IndexOf(demoPrefix) == 0)
            {
                return PrefixType.Demo;
            }
            else if (System.Web.HttpContext.Current.Request.Url.ToString().ToLower().IndexOf(vssPrefix) == 0)
            {
                return PrefixType.Vss;
            }
            return PrefixType.Formal;
        }

        public static bool IsDemo()
        {
            return GetDemoPrefix() != PrefixType.Formal;
        }

        private static HttpType CurrentHttpType
        {
            get
            {
                return System.Web.HttpContext.Current.Request.IsSecureConnection ? HttpType.Https : HttpType.Http;
            }
        }

    }


    public class WebReFile
    {

        public static string GetJavascriptString(FileItem fileItem)
        {
            return string.Format("<script language=\"javascript\" src=\"{0}\"></script>", Urls.BuildJs(fileItem));
        }

        public static string GetCssString(FileItem fileItem)
        {
            return string.Format("<link href=\"{0}\" rel=\"stylesheet\" type=\"text/css\" />", Urls.BuildCss(fileItem));
        }

        public static string GetMetadataLink(MetaDataLink link)
        {
            StringBuilder sb = new StringBuilder();
            if (link != null)
            {
                sb.Append("<link");
                if (!string.IsNullOrEmpty(link.Rel))
                {
                    sb.AppendFormat(" rel=\"{0}\"", HttpUtility.HtmlEncode(link.Rel));
                }
                if (!string.IsNullOrEmpty(link.Type))
                {
                    sb.AppendFormat(" type=\"{0}\"", HttpUtility.HtmlEncode(link.Type));
                }
                if (!string.IsNullOrEmpty(link.Title))
                {
                    sb.AppendFormat(" title=\"{0}\"", HttpUtility.HtmlEncode(link.Title));
                }
                if (!string.IsNullOrEmpty(link.Href))
                {
                    sb.AppendFormat(" href=\"{0}\"", HttpUtility.HtmlEncode(link.Href));
                }
                if (!string.IsNullOrEmpty(link.Media))
                {
                    sb.AppendFormat(" media=\"{0}\"", HttpUtility.HtmlEncode(link.Media));
                }
                sb.Append(" />");
            }
            return sb.ToString();
        }
    }  

}
